using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using MbUnit.Core.Framework;
using MbUnit.Framework;
using Doubler.Templates.Recorder;
using Doubler.Logging;
using TestAssistance;

namespace Test.RecorderGenerator
{
    [TestFixture]
    public class TestRecorderGenerator
    {
        public Reflector.Framework.ReflectorServices _Services;
        
        private Reflector.Framework.ReflectorServices Services
        {
            get
            {
                if (_Services == null)
                    _Services = ReflectorAssistant.ReflectorServices;
                return _Services;	
            }
        }

        private Reflector.CodeModel.ITypeDeclaration GetReflectorSubjectType(Type systemSubjectType)
        {
            this.Services.AssemblyManager.LoadFile(systemSubjectType.Assembly.Location);
            this.Services.AssemblyManager.LoadFile(typeof(Object).Assembly.Location);
            Reflector.CodeModel.ITypeDeclaration ReflectorSubjectType = this.Services.Search.FindType(systemSubjectType);
            return ReflectorSubjectType;
        }

        private void Compare(RecorderTemplate testSubject, string resourceFile)
        {
            using (FileStream ActualStream = new FileStream(testSubject.OutputFilePath, FileMode.Open))
            {
                using (Stream ExpectedStream = this.GetType().Assembly.GetManifestResourceStream(resourceFile))
                {
                    FileAssert.AreStreamContentEqual(ExpectedStream, ActualStream);
                }
            }
        }

        [Test]
        public void GenerateUnitTestForOrder()
        {
            Type SystemSubjectType = typeof(TestSubject.SomethingBase);
            Reflector.CodeModel.ITypeDeclaration ReflectorSubjectType = this.GetReflectorSubjectType(SystemSubjectType);
            
            RecorderTemplate TestSubject = new RecorderTemplate(Refly.CodeDom.CodeLanguage.Cs, @"C:\Temp", ReflectorSubjectType, this.Services);
            
            LogClassFactory.Logger.LogEvent += new LogEventHandler(LogMessage);
            
            TestSubject.OverrideOverrideables = true;
            TestSubject.OutputLanguage = Refly.CodeDom.CodeLanguage.Cs;
            TestSubject.Namespace = "Double";

            File.Delete(TestSubject.OutputFilePath);
            
            TestSubject.Generate();
            
            string ResourceFile = "Test.RecorderGenerator.RecorderSomethingBase.Cs";
            this.Compare(TestSubject, ResourceFile);
        }

        [Test]
        public void GenerateUnitTestForIMyInterface2()
        {
            Type SystemSubjectType = typeof(IMyInterface2);
            Reflector.CodeModel.ITypeDeclaration ReflectorSubjectType = this.GetReflectorSubjectType(SystemSubjectType);

            RecorderTemplate TestSubject = new RecorderTemplate(Refly.CodeDom.CodeLanguage.Cs, @"C:\Temp", ReflectorSubjectType, this.Services);

            LogClassFactory.Logger.LogEvent += new LogEventHandler(LogMessage);

            TestSubject.OutputLanguage = Refly.CodeDom.CodeLanguage.Cs;

            File.Delete(TestSubject.OutputFilePath);

            TestSubject.Generate();

            string ResourceFile = "Test.RecorderGenerator.RecorderIMyInterface2.Cs";
            this.Compare(TestSubject, ResourceFile);
        }

        public void LogMessage(String message)
        {
            Console.WriteLine(message);
        }
    }
}
